/**
	@author yuntao zhou
	require js/formatter/qs_formatter_en_us.js
	require js/language/qs_gla_en_us.js
	require widgets/common.js
**/

if(!QSAPI){
	var QSAPI={};
}

if(!QSAPI.Widget){
	QSAPI.Widget={};
}

QSAPI.Widget.ListView=function(container,columns,config,callback){
	this.container=container;
    this.callback=callback||{};
    this.columns=columns;
    this.config={
        gConfig:{
            column:this.columns,
            indexColKey:'gkey',
            autoWidth: true,
            lazyLoad: false,
            sortable: true,
            cellResize: false,
            cellRearrange: false,
            alternateRow: false,
            autoHeight:true
        },
        title:QS_Lang.marketGLA.title,
        hideTitle:false,
		isFormat:true,
		accuracy:2,
		volaccuracy:4,
		exch:null,
		token:null
    };
    if(config){
        $.extend(true,this.config,config);
    }
    this.el=null;
    this.html='<div class="qs-listview">'+(this.config.hideTitle?'':'<div class="qs-listview-hd">'+this.config.title+'</div>')+'<div class="qs-listview-bd"></div></div>';
    this.grid=null;
    this.contentKey = document.body.textContent ? 'textContent' : 'innerText';
    this.init();
};

QSAPI.Widget.ListView.prototype={
	init:function(){
        this.el=$(this.html).appendTo(this.container);
        this._createGrid();
    },
    _createGrid:function(){
        this.grid=new Util.DataGrid(this.el.find('.qs-listview-bd'),null,this.config.gConfig,this.callback);
    },
    reBindData:function(data){
        this.grid.reBindData(data);
    },
    getDOMMap:function(){
        return this.grid.DOMMap;
    },
    getIDs:function(){
        return this.grid.getIDs();
    },
    setDataToRow:function(key,data){
        if (this.grid.rows[key]) {
            $.extend(this.grid.rows[key].data, data);
        }
    },
    highlightColumn:function(dataIndex){
        this.grid.highlightColumn(dataIndex, true);
        var headCells=this.grid.header.row.cells;
        for(var c in headCells){
            if(c==dataIndex){
                this.grid.header.row.cells[c].el.addClass('rtq-grid-cell-highlight');
            }else{
                this.grid.header.row.cells[c].el.removeClass('rtq-grid-cell-highlight');
                this.grid.header.row.cells[c].el.removeClass('rtq-grid-cell-highlight');
            }
        }
    },
    updateData:function(key,aData){
        if(!this.grid.DOMMap[key]){
            return;
        }
        var gValue,oValue;
        if(aData["Chg"]){
        	if(!this._isValidRecord(aData["Chg"].value,this.config.token)){
            	this.grid.removeRow(key);
            	return;
            }
            aData["UpDown"]=aData["Chg"];
        }
        for (var field in aData) {
            if (!aData.hasOwnProperty(field)||!aData[field]) {
                continue;
            }
            gValue = aData[field].value;
            oValue = aData[field].oValue;
            if (typeof gValue != 'undefined' && gValue != null) {
                this.updateDOM(key, field, gValue, oValue);
            }
        }
    },
    updateDOM:function(key, field, data, oldData){
        var el = this.grid.DOMMap[key]?this.grid.DOMMap[key][field]:null;
        if(!el){
            return;
        }
        var ellipsis=false;
		var accuracy=this.config.accuracy;
		var isFormat=this.config.isFormat;
		var formatData=data;
        switch (field) {
            case 'LastPrice':
               formatData = QS_NumFmt.Fmt2(data,false,"",accuracy,isFormat); //data.toFixed(accuracy);
               Util.flash(el,null,null);
               break;
            case 'UpDown':
                var cls = data >= 0 ? "up" : "down";
                formatData = '<a class="updown ' + cls + '" href="javascript:void(0)">' + cls + '</a>';
                break;
            case 'Chg':
            case 'Chg%':
                formatData=QS_NumFmt.Fmt2(data,false,"",accuracy,isFormat);//data.toFixed(accuracy);
                Util.flashWithDirectional(el,data,oldData,data>=0)
                break;
			case 'OpenPrice':
            case 'HighPrice':
            case 'LowPrice':
                formatData = QS_NumFmt.Fmt2(data,false,"",accuracy,isFormat);//data.toFixed(accuracy);
                break;
            case 'Volume':
                data = data / 1000000;
                formatData = QS_NumFmt.Fmt2(data,false,"",this.config.volaccuracy,isFormat);//data.toFixed(accuracy);
                Util.flash(el,null,null);
                break;
            case 'Name':
                ellipsis=true;
                formatData='<a href="javascript:void(0)" title="'+data+'" style="overflow:hidden;">'+data+'</a>';
                break;
            default:
                break;
        }
		this.grid.rows[key].cells[field].updateCell(formatData,data);
        /*if (!ellipsis) {
            el[contentKey] = data;
        }else{
            this.grid.rows[key].cells[field].updateCell(data);
        }*/
    },
    clear:function(){
        for(var p in this){
            if(!p || p == 'clear'){continue;}
            this[p] = null;
            delete this[p];
        }
    },
    empty:function(){
        this.grid.empty();
    },
    _isValidRecord:function(chg,token){// check the record to prevent negative values in gainers and positive values in loosens section
    	if((token=='D1902'||token=='D1900')&&chg<=0){
    		return false;
    	}
    	if((token=='D1903'||token=='D1901')&&chg>=0){
    		return false;
    	}
    	return true;
    }
};
